package com.huang.user.controller;

import com.huang.common.core.constant.CacheConstants;
import com.huang.common.core.context.SecurityContextHolder;
import com.huang.common.core.domain.R;
import com.huang.user.domin.User;
import com.huang.user.form.LoginBody;
import com.huang.security.module.LoginUser;
import com.huang.security.service.TokenService;
import com.huang.user.service.SysLoginService;
import com.huang.user.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @Author: huangzixiang
 * @CreateTime: 2023-01-09  14:49
 * @Description: TODO
 **/
@RestController
@RequestMapping("/api")
public class UserController {
    private final static long expireTime = CacheConstants.EXPIRATION;

    @Autowired
    private TokenService tokenService;
    @Autowired
    private SysLoginService sysLoginService;
    @Autowired
    private UserService userService;
    @RequestMapping("/test")
    public String UserTest(){
        return "userTest";
    }
    @PostMapping("/login")
    public R<?> login(@RequestBody LoginBody form)
    {
        // 用户登录
        LoginUser userInfo = sysLoginService.login(form.getUsername(), form.getPassword());
        // 获取登录token
        return R.ok(tokenService.createToken(userInfo));
    }
    @GetMapping("/getUser")
    public String  getUser(){

        String userName = SecurityContextHolder.getUserName();
        return userName;
    }
//    @GetMapping("/getStatus")
//    public Object getStatus(){
//        return status;
//    }
    @GetMapping("/getUserMapper")
    public R<?> getUserMapper(){
        return R.ok(userService.list());
    }


    @GetMapping("/testSeata")
    public R<?> testSeata(){
        userService.seataTest();
        return R.ok(userService.list());
    }
    @GetMapping("/addUser")
    Boolean addUser(String userName,Integer  age){
        User user = new User();
        user.setName(userName);
        user.setAge(age);
        return  userService.save(user);
    }

}
